using System;
using System.Data;
using System.Collections.Generic;
using System.IO;
using Mono.Data.Sqlite;
 
namespace alkyneDB
{
	public partial class MainWindow : Gtk.Window
	{
		public MainWindow () : base(Gtk.WindowType.Toplevel)                                           
		{
			this.Build ();
			
			List<FileInfo> dirs = akcore.scrapper.dir.scanDeep("/home/domagoj/Music/",new List<string>{ ".mp3"});
			
			this.Title = dirs.Count.ToString() + " files found";
			
			//firstIteration(dirs);
			secondIteration(dirs);
					                                                                                                                                          
		}
		private void firstIteration(List<FileInfo> dirs)
		{
			IDbConnection dbcon = (IDbConnection) new SqliteConnection("URI=file:/home/domagoj/Programming/XBMC/alkyneDB-Mono/database.db");
			dbcon.Open();
			
			IDbCommand dbcmd = dbcon.CreateCommand();
			dbcmd.CommandText = "CREATE TABLE IF NOT EXISTS music_songs (path TEXT, format TEXT);";
			dbcmd.ExecuteNonQuery();
			foreach (FileInfo fi in dirs)
			{
				dbcmd.CommandText = "INSERT INTO music_songs (path,format) VALUES (\"" + fi.FullName + "\",\"" + fi.Extension + "\");";
				dbcmd.ExecuteNonQuery();
			}
			
			dbcmd.Dispose();
			dbcmd = null;
			dbcon.Close();
			dbcon = null;
		}
		private void secondIteration(List<FileInfo> dirs)
		{
			IDbConnection dbcon = (IDbConnection) new SqliteConnection("URI=file:database.db");
			dbcon.Open();
			
			IDbCommand dbcmd = dbcon.CreateCommand();
			dbcmd.CommandText = "CREATE TABLE IF NOT EXISTS music_songs (path TEXT, format TEXT);";
			dbcmd.ExecuteNonQuery();
			
			dbcmd.Transaction = dbcon.BeginTransaction();			
			foreach (FileInfo fi in dirs)
			{
				dbcmd.CommandText = "INSERT INTO music_songs (path,format) VALUES (\"" + fi.FullName + "\",\"" + fi.Extension + "\");";
				dbcmd.ExecuteNonQuery();
			}
			
			dbcmd.Transaction.Commit();
			
			dbcmd.Dispose();
			dbcmd = null;
			dbcon.Close();
			dbcon = null;
		}
	}
}

